﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using MSO = Microsoft.Office.Core;

using ConvergensInterfaces;
using System.Drawing;
using System.IO;
using System.Reflection;

namespace ConvergensDominoConnector
{
    public abstract class CDCOCommandBarButtonFactory : CommandBarButtonFactory
    {
        private CDCOCommandBarButtonEvents commandBarButtonEvents = null;

        internal class AxHostConverter : System.Windows.Forms.AxHost
        {
            private AxHostConverter() : base("") { }

            static public stdole.IPictureDisp ImageToPictureDisp(Image image)
            {
                return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);
            }
        }

        protected CDCOCommandBarButtonFactory(CDCOCommandBarButtonEvents commandBarButtonEvents)
        {
            this.commandBarButtonEvents = commandBarButtonEvents;

        }

        public MSO.CommandBarButton Create(MSO.CommandBar commandBar)
        {
            MSO.CommandBarButton commandBarButton;
            commandBarButton = (MSO.CommandBarButton)commandBar.Controls.Add(MSO.MsoControlType.msoControlButton, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, true);
            commandBarButton.Caption = "Journaliser";
            commandBarButton.DescriptionText = "CDCODescriptionText";
            commandBarButton.Style = MSO.MsoButtonStyle.msoButtonIconAndCaptionBelow;
            //this.commandBarButton.Mask = buttonPicture;
            commandBarButton.Click += new MSO._CommandBarButtonEvents_ClickEventHandler(commandBarButtonEvents.Journalize);


            DirectoryInfo directory = new DirectoryInfo(System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\Convergens\\CDCO");
            if (!directory.Exists)
            {
                throw new Exception("Directory \"" + System.Reflection.Assembly.GetCallingAssembly().Location + "\" does not exist.");
            }

            string iconFilePath = directory.FullName + "\\icon.bmp";
            Image buttonImage = Image.FromFile(iconFilePath);
            stdole.IPictureDisp buttonPicture = AxHostConverter.ImageToPictureDisp(buttonImage);
            commandBarButton.Picture = buttonPicture;

            return commandBarButton;
        }

        public void Dispose(MSO.CommandBarButton buttons)
        {
            //if (buttons == null)
            //{
            //    return;
            //}
            //if (buttons.Length == 0)
            //{
            //    return;
            //}

            //int buttonIndex = 0;

            //if (this.journalize && buttons.Length > buttonIndex)
            //{
                try
                {
                    if (buttons != null)
                    {
                        buttons.Click -= new MSO._CommandBarButtonEvents_ClickEventHandler(commandBarButtonEvents.Journalize);
                        Marshal.ReleaseComObject(buttons);
                        buttons = null;
                    }
                }
                catch (Exception)
                {
                    //suppress
                }
                //++buttonIndex;
                //if (buttonIndex >= buttons.Length)
                //{
                //    return;
                //}
            ///}

        }
    }
}